<?php

/**
 * uf
 * 
 * @package 
 * @author Armin
 * @copyright Copyright (c) 2004
 * @version $Id: class_uf.inc.php,v 1.19 2005/08/01 10:16:40 phppc Exp $
 * @access public
 **/
class uf extends tpl {
	var $uf_field = array(); // Array with the main information of the form data
	var $uf_input = array(); //
	
	/**
	 * uf::uf_make_input()
	 * 
	 * @param $start
	 * @param $ende
	 * @param $check
	 * @param $name
	 * @param integer $size
	 * @param string $var
	 * @return 
	 **/
	function uf_make_input($start, $ende, $check, $name, $size = 1, $var = "") {
		$input_parts = "";
		for ($start; $start <= $ende; $start ++) {
			if ($start == $check) {
				$checked = " selected=\"selected\" ";
			}else $checked = "";
			if ($var == "") {
				$value = $start;
			} else
				$value = $$var[$start];
			$input_parts .= "<option $checked value=\"$start\">$value</option>";
		}
		$input = "<select name=\"$name\" size=\"$size\">$input_parts</select>";
		return $input;
	}
	/**
	 * uf::uf_make_input_db()
	 * 
	 * @param $abfrage
	 * @param $check
	 * @param $var_field
	 * @param $name_field
	 * @param $form_var
	 * @param integer $size
	 * @param string $default_value
	 * @param string $default_name
	 * @return 
	 **/
	function uf_make_input_db($abfrage, $check, $var_field = "id", $name_field = "name", $form_var, $size = 1, $default_value = "", $default_name = "") {
		$erg = $this->db_abfrage_q($abfrage);
		$input_parts = "";
		if ($default_name != "") {
			if($check == $default_value) $checked = " selected";	
			$input_parts = "<option value=\"$default_value\"$checked>$this->language[$default_name]</option>";#
		}
		while ($result = $this->db_abfrage_fetch($erg)) {
			$res_var = $result[$var_field];
			if($check == $res_var) $checked = " selected";
			else $checked = "";
			$input_parts .= "<option value=\"$res_var\"$checked>$result[$name_field]</option>";
		} // while
		$input = "<select size=\"$size\" name=\"$form_var\">$input_parts</select>";
		return $input;
	}
	/**
	 * uf::uf_make_input_name()
	 * 
	 * @param $start
	 * @param $ende
	 * @param $check
	 * @param $lang_name
	 * @param $form_var
	 * @param integer $size
	 * @param string $style_array
	 * @return 
	 **/
	function uf_make_input_name($start, $ende, $check, $lang_name, $form_var, $size = 1, $style_array = "") {
		$checked[$check] = " selected=\"selected\"";
		$input_parts = "";
		for ($start; $start <= $ende; $start ++) {
			$style = "";
			if(!isset($checked[$start])) $checked[$start] = "";		
			if (isset($style_array[$start])) $style = " class=\"".$style_array[$start]."\"";
			$input_parts .= "<option value=\"$start\"$checked[$start]$style>[LANG=$lang_name$start]</option>";
		}
		return "<select size=\"$size\" name=\"$form_var\">$input_parts</<select>";
	}
	/**
	 * uf::uf_make_input_lang()
	 * 
	 * @param $check
	 * @param integer $size
	 * @param string $form_name
	 * @return 
	 **/
	function uf_make_input_lang($check, $size = 2, $form_name = "lang") {
		$handle = opendir($this->config["a_pfad"].$this->config["sub_dir_lang"]);
		$files = array ();
		while ($file = readdir($handle)) {
			array_push($files, $file);
		}
		closedir($handle);
		sort($files);
		$lang_input_one = "";
		foreach ($files as $file) {
			$lang_pattern = "!([a-z_0-9_])(.inc.php)!i";
			$valid_lang_file = preg_match($lang_pattern, $file);
			$file_name = preg_replace($lang_pattern, "\\1", $file);
			if($file_name == $check) $checked = " selected=\"selected\"";
			else $checked = "";
			if (($file != "." AND $file != "..") AND ($valid_lang_file == true)) {
				$lang_input_one .= "<option value=\"$file_name\"$checked>$file_name</option>";
			}
		}
		$lang_input = "<select size=\"$size\" name=\"$form_name\">$lang_input_one</select>";
		return $lang_input;
	}
	/**
	 * uf::uf_link_nav_creator()
	 * 
	 * @param $check
	 * @param $lang
	 * @param $var_name
	 * @param $array
	 * @param $link
	 * @param $temp
	 * @param $temp_one
	 * @param string $class_aktiv
	 * @param string $class_normal
	 * @param string $mode
	 * @param string $opt_link
	 * @param string $tpl_dir
	 * @return 
	 **/
	function uf_link_nav_creator($check, $lang, $var_name, $array, $link, $temp, $temp_one, $class_aktiv = "nc_aktiv", $class_normal = "nc", $mode = "not_used", $opt_link = "not_used", $tpl_dir = "") {
		$navs_cr = "";
		foreach ($array as $start => $value) {
			$_blank = "";
			$nav_link = $link."&amp;".$var_name."=".$array[$start];
			if ($value == $check)$class = $class_aktiv;
			else $class = $class_normal;
			$act = $value;
			if(isset($mode[$start])) {
				if ($mode[$start] == "extern" OR $mode[$start] == "extern_nw") $nav_link = $opt_link[$start];
				if ($mode[$start] == "extern_nw") $_blank = " target=\"_blank\"";
			}
			$this->tpl_load("$temp_one", $tpl_dir);
			$this->tpl_replace_vars(array ("class" => $class, "nav_link" => $nav_link, "_blank" => $_blank, "lang" => $lang, "act" => $act,"start" => $start));
			$navs_cr .= $this->tpl_output();
		}
		$this->tpl_load("$temp", $tpl_dir);
		$this->tpl_replace_vars(array ("navs_cr" => $navs_cr));
		$nav_cr = $this->tpl_output();
		return $nav_cr;
	}
	/**
	 * uf::uf_input_creator()
	 * 
	 * @param $var_array
	 * @param $lang
	 * @param string $tpl_text
	 * @param string $tpl_one
	 * @param string $tpl_dir
	 * @param string $only_ids
	 * @param string $tpl_textarea
	 * @return 
	 **/

	function uf_input_creator($var_array, $lang, $tpl_text = "text", $tpl_one = "icreator_one", $tpl_dir = "uf/", $only_ids = "", $tpl_textarea = "textarea") {
		$tpl_inputs = "";
		foreach ($this->uf_field as $start => $input_name) {
			if ((is_array($only_ids) AND $this->uf_form_conf_def($start, $only_ids, "", "valid_input") == 1) OR ($only_ids == "")) {
				if(isset($var_array["$input_name"]) AND is_array($var_array)) $input_value = $var_array["$input_name"];
				else $input_value = "";
				if(!isset($this->uf_input[$start]["type"])) $this->uf_input[$start]["type"] = "text";
				$input = $this->uf_input[$start];
				if ($input["type"] == "input_db") {
					if(!isset($input["id"])) $input["id"] = "id";
					if(!isset($input["name"])) $input["name"] = "name";
					$input_one = $this->uf_make_input_db($input["abfrage"], $input_value, $input["id"], $input["name"], $input_name, $size = 1);
				}
				elseif ($input["type"] == "input") $input_one = $this->uf_make_input($input["start"], $input["ende"], $input_value, $input_name, $input["size"]);
				elseif ($input["type"] == "input_name" OR $input["type"] == "yes_no") {
					if ($input["type"] == "yes_no") {
						$lang_var = "yes_no_";
						$input["start"] = 0;
						$input["ende"] = 1;
						if (!isset($input["style"]["type"])) {
							$input["style"][0] = $this->config["product_short"]."_red_bg";
							$input["style"][1] = $this->config["product_short"]."_green_bg3";
						}
					} else {
						$lang_var = $lang.$start."_";
						if (isset($input["style"]["type"]) AND $input["style"]["type"] == "standard") {
							for ($i = $input["start"]; $i <= $input["ende"]; $i ++) {
								if ($i == 0)
									$input["style"][$i] = $this->config["product_short"]."_red_bg";
								else
									$input["style"][$i] = $this->config["product_short"]."_green_bg$i";
							}
						}
					}
					if (!isset($input["size"]) OR $input["size"] < 1)	$input["size"] = 1;
					if(!isset($input["start"]) OR $input["start"] < 0) $input["start"] = 0;
					if(!isset($input["ende"]) OR $input["ende"] < 1) $input["ende"] = 1;
					if(!isset($input["style"])) $input["style"] = "";
					$input_one = $this->uf_make_input_name($input["start"], $input["ende"], $input_value, $lang_var, $input_name, $input["size"], $input["style"]);
				}
				elseif ($input["type"] == "input_lang") $input_one = $this->uf_make_input_lang($input_value);
				elseif ($input["type"] == "kat") {
					if (!isset($input["set_first"]) OR $input["set_first"] != -1) $input["set_first"] = 1;
					if (!isset($input["dont_show"]) OR $input["dont_show"] < 1) $input["dont_show"] = -1;
					if (!isset($input["size"]) OR $input["size"] < 0) $input["size"] = 1;
					$this->kat_rek(0, "input", $input_value, $input["dont_show"], $input["set_first"], $input_name, $input["size"], $input["first_lang"]);
					$input_one = $this->kat_return_value();
				}
				elseif ($input["type"] == "textarea") {
					$this->tpl_load($tpl_textarea, $tpl_dir);
					$this->tpl_replace_vars(array ("input_name" => $input_name, "cols" => $input["cols"], "rows" => $input["rows"], "input_value" => $input_value));
					$input_one = $this->tpl_output();
				}
				elseif ($input["type"] == "manually") $tpl_inputs .= $var_array["$input_name"];
				else {
					$this->tpl_load($tpl_text, $tpl_dir);
					if(!isset($input["ml"])) $input["ml"] = "";
					if(!isset($input["size"])) $input["size"] = "";
					$this->tpl_replace_vars(array ("itype" => $input["type"], "input_name" => $input_name, "input_value" => $input_value, "size" => $input["size"], "ml" => $input["ml"]));
					$input_one = $this->tpl_output();
				}
//				if(isset($input["colspan"])) $colspan = 1;
//				if ($input["colspan"] < 1) $colspan = 1;
				if ($input["type"] != "manually") {
					$this->tpl_load($tpl_one, $tpl_dir);
					$this->tpl_replace_vars(array("lang" => $lang,"start" => $start,"input_one" => $input_one));
					$tpl_inputs .= $this->tpl_output();
				}
				unset ($input_name, $input_value, $input_field, $input, $input_one);
			}
		}
		return $tpl_inputs;
	}
	/**
	 * uf::uf_create_array_conf()
	 * 
	 * @param string $abfrage_type
	 * @param string $only_ids
	 * @param string $input_type
	 * @param integer $calc_anzahl
	 * @desc Proves form vars and creates a sql query
	 * @return 
	 **/
	function uf_create_array_conf($abfrage_type = "update", $only_ids = "", $calc_anzahl = 0) {
		$return = array ();
		$anzahl = 0;
		if ($calc_anzahl == 1) {
			$only_ids = array ();
			foreach ($this->uf_field as $start => $field) {
				if ($this->uf_input[$start]["no_db"] != 1) array_push($only_ids, $start);
			}
		}
		$abfrage = "";
		$abfrage2 = "";
		$anzahl = count($this->uf_field);
		$anzahl_c = count($only_ids);
		$c_count = 0;
		$bisher = 0;
		$input = array();
		foreach ($this->uf_field as $start => $field) {
			if(!isset($start)) echo $field;
			$bisher ++;
			if ((is_array($only_ids) AND $this->uf_form_conf_def($start, $only_ids, "", "valid_input") == 1) OR ($only_ids == "")) {
				if(!isset($_POST["$field"])) $_POST["$field"] = "";
				if(isset($this->uf_input[$start])) $input = $this->uf_input[$start];
				if(!isset($input["optional"])) $input["optional"] = 0;
				if(!isset($input["type"])) $input["type"] = "type";
				if(!isset($input["no_db"])) $input["no_db"] = 0;
				$c_count ++;
				if ($input["optional"] != 1 AND $input["type"] != "manually") array_push($return, $field);
				if ($input["no_db"] != 1) {
					if ($abfrage_type == "update") $abfrage .= " $field=\"$_POST[$field]\"";
					elseif ($abfrage_type == "insert") {
						$abfrage .= "$field";
						$abfrage2 .= "\"".$_POST["$field"]."\"";
					}
					if (($bisher < $anzahl) AND (($c_count < $anzahl_c) OR ($only_ids == ""))) {
						if ($abfrage_type == "update" OR $abfrage_type == "insert")
							$abfrage .= ",";
						if ($abfrage_type == "insert")
							$abfrage2 .= ",";
					}
				}
			}
		}
		return array ($return, $abfrage, $abfrage2);
	}
	/**
	 * uf::uf_create_hidden_form()
	 * 
	 * @param array $hidden_array
	 * @return 
	 **/
	function uf_create_hidden_form($hidden_array = array ()) {
		$input = "";
		if(!empty($hidden_array)) {
			foreach ($hidden_array as $name => $value) {
				if($name != "") $input .= "<input type=\"hidden\" name=\"".$name."\" value=\"".$value."\" /> ";
			}
		}	
		foreach ($this->module_extern_array as $intern_name => $extern_name) {
			$module_ignore = "";
			if($this->uf_form_conf_def($intern_name,$module_ignore,"","valid_input") != 1) $input .= "<input type=\"hidden\" name=\"".$extern_name."\" value=\"".$this->module_new_value["$intern_name"]."\" />";
		}
		return $input."<input type=\"hidden\" name=\"".$this->config["ncs_var"]."\" value=\"".$this->ncs."\" />";
	}
	function uf_form_conf($input_array, $array = "") { 
		if (!is_array($array)) $array = $_POST;
		foreach ($input_array as $input_feld) {
			if ($array["$input_feld"] != "") {
				$valid_input = 1;
			} else {
				$valid_input = 0;
				break;
			}
		}
		return $valid_input;
	}

	function uf_form_conf_def($var, $value_array, $default_value = "", $return = "") {
		$valid_input = 0;
		if (is_array($value_array)) {
			foreach ($value_array AS $value) {
				if ($var == $value) {
					$valid_input = 1;
					break;
				}
			}
		}
		elseif ($value_array == "is_numeric") {
			if (is_numeric($var))
				$valid_input = 1;
		}
		if ($return == "valid_input") {
			$var = $valid_input;
		} else {
			if ($valid_input == 1)
				$var = $var;
			elseif ($default_value != "") $var = $default_value;
			else $var = 0;
		}
		
		return $var;
	}
	/**
	 * uf::create_where_part()
	 * Erstellt den WHERE-Teil einer SQL-Abfrage
	 * @param $array
	 * @param string $id_name
	 * @return 
	 **/
	function uf_create_where_part($array, $id_name = "id") {
		$anzahl = count($array);
		$start = 1;
		$abfrage = "";
		foreach ($array as $id) {
			$abfrage .= $id_name." = \"".$id."\"";
			if ($start < $anzahl)
				$abfrage .= " OR ";
			$start ++;
		}
		return $abfrage;
	}
	/**
	 * uf::uf_secure()
	 * 
	 * @param $array
	 * @return 
	 **/
	function uf_secure($array) {
		foreach ($array as $array_name => $value) {
			if(!is_array($value))$array["$array_name"] = stripslashes(htmlentities($value));
		}
		return $array;
	}
	/**
	 * uf::uf_create_complete_form()
	 * 
	 * @param $lang_title lang string of the tables title
	 * @param $hidden_array
	 * @param $lang_submit
	 * @param $lang_reset
	 * @param $tpl_compl
	 * @param $tpl_c_dir
	 * @param $form_action
	 * @param $field_array
	 * @param $input
	 * @param $var_array
	 * @param $input_type
	 * @param $lang
	 * @param string $tpl_text
	 * @param string $tpl_one
	 * @param string $tpl_dir
	 * @param string $only_ids
	 * @param string $tpl_textarea
	 * @param integer $colspan
	 * @param string $zusatz
	 * @param string $form_name
	 * @param string $form_zusatz
	 * @return 
	 **/
	function uf_create_complete_form($lang_title, $hidden_array, $lang_submit, $lang_reset, $tpl_compl, $tpl_c_dir, $form_action, $var_array, $lang, $tpl_text = "text", $tpl_one = "icreator_one", $tpl_dir = "uf/", $only_ids = "", $tpl_textarea = "textarea", $colspan = 1, $zusatz = "", $form_name = "nc", $form_zusatz = "") {
		$inputs = $this->uf_input_creator($var_array, $lang, $tpl_text, $tpl_one, $tpl_dir, $only_ids, $tpl_textarea);
		$hidden_fields = $this->uf_create_hidden_form($hidden_array);
		if ($colspan < 2) $colspan = 2;
		$this->tpl_load($tpl_compl, $tpl_c_dir);
		$this->tpl_replace_vars(array("zusatz" => $zusatz,"form_action" => $form_action,"form_name" => $form_name,"form_zusatz" => $form_zusatz,"hidden_fields" => $hidden_fields,"inputs" => $inputs,"colspan" => $colspan,"lang_submit" => $lang_submit,"lang_reset" => $lang_reset,"lang_title" => $lang_title));
		$tpl_main = $this->tpl_output();
		return $tpl_main;
	}

}

?>